<?php

namespace Modules\Product;

use Modules\Product\Entities\Product;
use \Modules\Core\Entities\SubscribeRecord;

class Cron
{
	public function handle($schedule)
	{
		$that = $this;
		$schedule->call(function() use ($that) {
			try {
				$that->everyMinute();
			}
			catch (\Throwable $e) {
                \Log::error('cron任务异常');
                \Log::error($e->getMessage());
            }
		})->everyMinute();
	}

	public function everyMinute()
	{
		// 

		Product::where('is_presell', 1)->where('presell_date', '<', date('Y-m-d H:i:s'))->update([
			'is_presell' => 0
		]);


		$list = Product::where('is_presell', 1)->where('presell_date', '<', date('Y-m-d H:i:s', strtotime('+70 seconds')))->get();
		foreach ($list as $item) {
			$all = SubscribeRecord::where([
                'target_uuid' => $item->uuid,
                'message_type' => 'presell_start',
                'status' => 0
            ])->get();

            foreach ($all as $message) {
            	$message->user->notify(new \Modules\Product\Notifications\PresellStart($item));

            	$message->update([
            		'status' => 1
            	]);
            }
		}
	}
}